package ezXpns.GUI;

import java.awt.BorderLayout;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import net.miginfocom.swing.MigLayout;
import javax.swing.JLabel;
import javax.swing.JTextField;
import com.toedter.calendar.JDateChooser;

import ezXpns.GUI.controllers.BudgetChangeController;
import ezXpns.GUI.controllers.RequestResult;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JComboBox;


/**
 * @author A0086931X
 */

//@author A0086931X
public class AdvancedSearchDialog extends JDialog {
	/**
	 * Create the dialog.
	 */
	public AdvancedSearchDialog(BudgetChangeController controller) {
		setAlwaysOnTop(true);
		this.controller = controller;
		setResizable( false );
		setTitle("Advanced Search");
		setBounds(100, 100, 251, 255);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new MigLayout("", "[fill][289.00,grow,fill][]", "[][][][][][][][58.00,grow,fill]"));
		{
			JLabel lblName = new JLabel("Name");
			contentPanel.add(lblName, "cell 0 0,alignx trailing");
		}
		{
			txtName = new JTextField();
			contentPanel.add(txtName, "cell 1 0,growx");
			txtName.setColumns(10);
		}
		{
			JLabel lblMinAmount = new JLabel("Min amount");
			contentPanel.add(lblMinAmount, "cell 0 1,alignx trailing");
		}
		{
			txtMinAmount = new JTextField();
			contentPanel.add(txtMinAmount, "cell 1 1,growx");
			txtMinAmount.setColumns(10);
		}
		{
			JLabel lblMaxPrice = new JLabel("Max amount");
			contentPanel.add(lblMaxPrice, "cell 0 2,alignx trailing");
		}
		{
			txtMaxAmount = new JTextField();
			txtMaxAmount.setColumns(10);
			contentPanel.add(txtMaxAmount, "cell 1 2,growx");
		}
		{
			JLabel lblType = new JLabel("Type");
			contentPanel.add(lblType, "cell 0 3");
		}
		{
			cbxType = new JComboBox<String>();
			contentPanel.add(cbxType, "cell 1 3,growx");
		}
		{
			JLabel lblFrom = new JLabel("From Date");
			contentPanel.add(lblFrom, "cell 0 4");
		}
		{
			dcMinDate = new JDateChooser();
			contentPanel.add(dcMinDate, "cell 1 4,grow");
		}
		{
			JLabel lblTo = new JLabel("To Date");
			contentPanel.add(lblTo, "cell 0 5");
		}
		{
			dcMaxDate = new JDateChooser();
			contentPanel.add(dcMaxDate, "cell 1 5,grow");
		}
		{
			JLabel lblTags = new JLabel("Tags");
			contentPanel.add(lblTags, "cell 0 6,alignx trailing");
		}
		{
			txtTag = new JTextField();
			contentPanel.add(txtTag, "cell 1 6,growx");
			txtTag.setColumns(10);
		}
		{
			JButton btnClear = new JButton("Clear");
			btnClear.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					txtName.setText("");
					txtMinAmount.setText("");
					cbxType.setSelectedItem(null);
					dcMinDate.setDate(null);
					dcMaxDate.setDate(null);
					txtTag.setText("");
					
				}
			});
			contentPanel.add(btnClear, "cell 0 7");
		}
		{
			JButton btnSearch = new JButton("Search");
			btnSearch.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					doAdvSearch();
				}
			});
			contentPanel.add(btnSearch, "cell 1 7");
		}
	}
	
	public void setAvailableTypes(String[] types) {
		cbxType.setModel(new DefaultComboBoxModel<String>(types));
		cbxType.setSelectedItem(null);
	}
	
	private void doAdvSearch(){
		RequestResult result = controller.requestSearch(
			nullIfEmpty(txtName.getText()),
			nullIfEmpty(txtMinAmount.getText()),
			nullIfEmpty(txtMaxAmount.getText()),
			dcMinDate.getDate(),
			dcMaxDate.getDate(),
			(String)cbxType.getSelectedItem(),
			nullIfEmpty(txtTag.getText())
		);
		if(!result.isAccepted()) {
			JOptionPane.showMessageDialog(null, result.getError());	
		}
	}
	
	private String nullIfEmpty(String str) {
		if(str.isEmpty())
			return null;
		else
			return str;
	}
	
	private static final long serialVersionUID = 1L;
	private JPanel contentPanel = new JPanel();
	private JTextField txtName;
	private JTextField txtMinAmount;
	private JTextField txtTag;
	private JComboBox<String> cbxType;
	private JDateChooser dcMinDate;
	private JDateChooser dcMaxDate;
	private BudgetChangeController controller;
	private JTextField txtMaxAmount;
}
